package com.sofwin.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sofwin.dto.ResourceDto;
import com.sofwin.pojo.SysResource;
import com.sofwin.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/resource")
public class ResourceController {

    @Autowired
    private ResourceService service;
   /**
    * @Description: 树形图封装数据(同步)
    * @Author: Mr.jhx
    * @Date: 2021/4/17 14:29
    * @Param: * @param model:
    * @Return: * @return: java.lang.String
    *
    **/
    @RequestMapping("data")
    public String data(Model model) throws JsonProcessingException {
//        List<ResourceDto> resources = service.selectAllResource();
//        ObjectMapper mapper = new ObjectMapper();
//        String str = mapper.writeValueAsString(resources);
//        model.addAttribute("str",str);
   //     List<ResourceDto> resources = service.selectAllResource();
     //   model.addAttribute("resources",resources);
        return "resource/data";
    }
    /**
     * @Description:获取树的数据（异步）
     * @Author: Mr.jhx
     * @Date: 2021/4/17 14:30
     * @Param: * @param flag:
     * @param roleId:
     * @Return: * @return: java.util.List
     **/
    @RequestMapping("treeData")
    @ResponseBody
    public List treeData(Integer flag,Integer roleId){
        if (flag!=null && flag==1){
//          List<ResourceDto> resources = service.selectResourceByRoled(roleId);
            List<SysResource> resources = service.selectResourceByRoleId(roleId);
            return resources;
        }
        List<SysResource> resources = service.selectAllResource2();
        return resources;
    }
    /**
     * @Description: 保存数据到树
     * @Author: Mr.jhx
     * @Date: 2021/4/17 14:31
     * @Param: * @param resource:
     * @Return: * @return: java.util.Map
     **/
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Map save( SysResource resource){
        Map map = new HashMap();
        boolean status = service.save(resource);
        map.put("status",status);
        return map;
    }


    /**
     * @Description:查询数据放到form表单
     * @Author: Mr.jhx
     * @Date: 2021/4/17 14:31
     * @Param: * @param id:
     * @Return: * @return: com.sofwin.pojo.SysResource
     **/
    @RequestMapping(value ="{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public SysResource getResource(@PathVariable Integer id ){
        SysResource resource = service.selectById(id);
        return resource;
    }
    /**
     * @Description:删除树节点
     * @Author: Mr.jhx
     * @Date: 2021/4/17 14:32
     * @Param: * @param id:
     * @Return: * @return: java.util.Map
     **/
    @RequestMapping(value = "{id}" ,method = RequestMethod.DELETE)
    @ResponseBody
    public Map delete( @PathVariable Integer id){
        Map map = new HashMap();
        boolean status = service.deleteById(id);
        map.put("status",status);
        return map;
    }



}
